/**
 * Created by zhangjinrui on 17/6/27.
 */

public class Solution81 {

    public static void main(String args[]) {
        Solution81 s = new Solution81();
        System.out.println(s.search(new int[]{3, 1}, 1));
    }

    public boolean search(int[] nums, int target) {
        if(nums.length == 0){
            return false;
        }
        int l = 0;
        int r = nums.length - 1;

        while (l < r) {
            int mid = (l + r) / 2;
            if (target == nums[mid]) {
                return true;
            } else {
                if (nums[mid] > nums[r]) {
                    if (nums[l] <= target && target < nums[mid]) {
                        r = mid;
                    } else {
                        l = mid + 1;
                    }
                } else if(nums[mid] < nums[r]){
                    if (nums[mid] < target && target <= nums[r]) {
                        l = mid + 1;
                    } else {
                        r = mid;
                    }
                } else{
                    r --;
                }
            }
        }
        return nums[l] == target;
    }
}
